var setting = {
    async: {
        enable: true,
        url: "../../Cloud/Dept/DeptAjaxHelper.ashx?method=GetDeptJsonData",
        autoParam: ["id", "name=n", "level=lv"],
        otherParam: { "otherParam": "zTreeAsyncTest" },
        dataFilter: filter
    },
    check: {
        enable: $("#hidIsMutiSelect").val() == "Y" ? true : false
    },
    view: {
        expandSpeed: "",
        addHoverDom: addHoverDom,
        removeHoverDom: removeHoverDom,
        dblClickExpand: false,
        nameIsHTML: true,
        selectedMulti: false
    },
    edit: {
        enable: true
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        beforeClick: beforeClick,
        beforeDrag: beforeDrag,
        beforeRemove: beforeRemove,
        beforeRename: beforeRename,
        onRemove: onRemove,
        onRename: onRename,
        onCheck: onCheck,
        onClick: onClick,
        beforeEditName: onBeforeEditName,
        onAsyncSuccess: function (event, treeId, treeNode, msg) { $("#treeLoading").hide(); }
    }
};
function onBeforeEditName(treeId, treeNode) {
    var name = treeNode.name;
    if (name.indexOf("(<span style='color:red'>") > -1) {
        name = name.substring(0, name.indexOf("(<span style='color:red'>"));
    }
    treeNode.name = name;
}
function beforeClick(treeId, treeNode) {
}

var selectDept_SelectedIDs = "";
var selectDept_SelectedNames = "";
var selectDept_SelectedFullNames = "";
function onClick(event, treeId, treeNode) {
    if ($("#hidIsMutiSelect").val() == "Y") { return; }
    var zTree = $.fn.zTree.getZTreeObj("divDeptTreeList"),
    nodes = zTree.getSelectedNodes(),
		selectDept_SelectedIDs = "";
    selectDept_SelectedNames = "";
    selectDept_SelectedFullNames = "";
    nodes.sort(function compare(a, b) { return a.id - b.id; });
    for (var i = 0, l = nodes.length; i < l; i++) {
        selectDept_SelectedNames += nodes[i].name + ",";
        selectDept_SelectedFullNames += nodes[i].fullDeptName + ",";
        selectDept_SelectedIDs += nodes[i].id + ",";
    }
    if (selectDept_SelectedNames.length > 0) selectDept_SelectedNames = selectDept_SelectedNames.substring(0, selectDept_SelectedNames.length - 1);
    if (selectDept_SelectedFullNames.length > 0) selectDept_SelectedFullNames = selectDept_SelectedFullNames.substring(0, selectDept_SelectedFullNames.length - 1);
    if (selectDept_SelectedIDs.length > 0) selectDept_SelectedIDs = selectDept_SelectedIDs.substring(0, selectDept_SelectedIDs.length - 1);
    if (isAddNode || isRemoveNode) { isAddNode = false; isRemoveNode = false; return; }//防止新增或删除操作时，刷新界面，造成体验差
    if (typeof (doSelectDeptCallback) == 'function') {
        doSelectDeptCallback(selectDept_SelectedIDs, selectDept_SelectedNames, selectDept_SelectedFullNames); //回调函数,在aspx页面时实现其方法
        //$("#divDeptPanel").fadeOut();
    }
    else {
        Dialog.error("选择组织分类失败,没有实现回调方法：doSelectDeptCallback");
    }
}
//多选
function onCheck(event, treeId, treeNode, clickFlag) {
    var zTree = $.fn.zTree.getZTreeObj("divDeptTreeList");
    var nodes = zTree.getCheckedNodes(true);
    selectDept_SelectedIDs = "";
    selectDept_SelectedNames = "";
    selectDept_SelectedFullNames = "";
    for (var i = 0; i < nodes.length; i++) {
        selectDept_SelectedIDs += nodes[i].id + ",";
        selectDept_SelectedNames += nodes[i].name + ",";
        selectDept_SelectedFullNames += nodes[i].fullDeptName + ",";
    }
    if (selectDept_SelectedIDs.length > 0) selectDept_SelectedIDs = selectDept_SelectedIDs.substring(0, selectDept_SelectedIDs.length - 1);
    if (selectDept_SelectedNames.length > 0) selectDept_SelectedNames = selectDept_SelectedNames.substring(0, selectDept_SelectedNames.length - 1);
    if (selectDept_SelectedFullNames.length > 0) selectDept_SelectedFullNames = selectDept_SelectedFullNames.substring(0, selectDept_SelectedFullNames.length - 1);
    if (isAddNode || isRemoveNode) { isAddNode = false; isRemoveNode = false; return; }//防止新增或删除操作时，刷新界面，造成体验差
    if (typeof (doSelectDeptCallback) == 'function') {
        doSelectDeptCallback(selectDept_SelectedIDs, selectDept_SelectedNames, selectDept_SelectedFullNames); //回调函数,在aspx页面时实现其方法
        //$("#divDeptPanel").fadeOut();
    }
    else {
        Dialog.error("选择组织分类失败,没有实现回调方法：doSelectDeptCallback");
    }
}
function filter(treeId, parentNode, childNodes) {
    if (!childNodes) return null;
    for (var i = 0, l = childNodes.length; i < l; i++) {
        childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
    }
    return childNodes;
}
var log, className = "dark";
function beforeDrag(treeId, treeNodes) {
    return false;
}

var isRemoveNode = false;
function beforeRemove(treeId, treeNode) {
    isRemoveNode = true;
    //return window.confirm("此操作将会删除当前组织下的所有组织和用户信息，确定删除吗?");
    ajaxTodo("../../Cloud/Dept/DeptAjaxHelper.ashx?method=DeleteDept&deptID=" + treeNode.id, function (json) {
        if (json.flag == "Y") {
            //alertMsg.correct("删除成功");
            var treeObj = $.fn.zTree.getZTreeObj("divDeptTreeList");
            treeObj.removeNode(treeNode);
            //删除成功后，回调清空方法
            if (typeof (doDeleteDeptCallback) == 'function') {
                doDeleteDeptCallback(treeNode.id); //回调函数,在aspx页面时实现其方法
                //$("#divTreePanel").fadeOut();
            }
        }
        else {
            Dialog.error(json.msg);
            return false;
        }
    });
    return false;
}
function beforeRename(treeId, treeNode, newName) {
    if (newName.length == 0) {
        alert("节点名称不能为空.");
        var zTree = $.fn.zTree.getZTreeObj("divDeptTreeList");
        setTimeout(function () { zTree.editName(treeNode) }, 10);
        return false;
    }
    treeNode.name = newName;
    var fullName = newName;
    if (treeNode.fullDeptName.lastIndexOf("/") > -1) {
        fullName = treeNode.fullDeptName.substring(0, treeNode.fullDeptName.lastIndexOf("/")) + "/" + newName;
    }
    treeNode.fullDeptName = fullName;
    return true;
}
//删除组织
function onRemove(e, treeId, treeNode) {
    //ajaxTodo("../Dept/DeptAjaxHelper.ashx?method=DeleteDept&deptID=" + treeNode.id, function (json) {
    //    if (json.flag == "Y") {
    //        //alertMsg.correct("删除成功");
    //    }
    //    else
    //        alertMsg.error("删除失败:"+json.msg);
    //});
}
//修改名称
function onRename(e, treeId, treeNode) {
    ajaxTodo("../../Cloud/Dept/DeptAjaxHelper.ashx?method=SaveSimpleData&deptID=" + treeNode.id + "&deptName=" + escape(treeNode.name), function (json) {
        if (json.flag == "Y") {
            //alertMsg.correct("修改成功");
        }
        else {
            Dialog.error(json.msg);
            return false;
        }
    });
}
var newCount = 1;
var isAddNode = false;
function addHoverDom(treeId, treeNode) {
    var sObj = $("#" + treeNode.tId + "_span");
    if (treeNode.editNameFlag || $("#addBtn_" + treeNode.id).length > 0) return;
    var addStr = "<button type='button' class='add' id='addBtn_" + treeNode.id+ "' title='新增' onfocus='this.blur();'></button>";
    sObj.after(addStr);
    var btn = $("#addBtn_" + treeNode.id);
    if (btn) btn.bind("click", function () {
        isAddNode = true;
        var zTree = $.fn.zTree.getZTreeObj("divDeptTreeList");
        var deptID = "";
        var count = newCount++;
        //新增子组织结点
        ajaxTodo("../../Cloud/Dept/DeptAjaxHelper.ashx?method=SaveSimpleData&parentDeptID=" + treeNode.id, function (json) {
            if (json.flag == "Y") {
                //alertMsg.correct("新增成功");
                deptID = json.deptID;
                zTree.addNodes(treeNode, { id: deptID, pId: treeNode.id, name: json.deptName,fullDeptName:json.fullDeptName, icon: "../../Cloud/Style/images/dept.png" });
                var newNodes = zTree.getNodeByParam("id", deptID);
            }
            else {
                Dialog.error(json.msg);
            }
        });
    });
};
function removeHoverDom(treeId, treeNode) {
    $("#addBtn_" + treeNode.id).unbind().remove();
};

$(function () {
    if ($("#hidAutoLoadTree").val() == "Y") {//打开页面就加载模式，另外一种是showMenu事件触发
        var objSelectTree = $.fn.zTree.init($("#divDeptTreeList"), setting);
        objSelectTree.setting.edit.renameTitle = "修改";
        objSelectTree.setting.edit.removeTitle = "删除";
        if ($("#hidIsMutiSelect").val() == "Y" || GetQueryString("isMutiSelect") == "Y") {
            objSelectTree.setting.check.chkboxType = { "Y": "", "N": "" };
        }
        else {
        }
    }
});
var _treetextboxID = "";
function showDeptMenu(id) {
    $("#treeLoading").show();
    if (id == "" || id == undefined || id == null) return;
    setting.async.url = "../../Cloud/Dept/DeptAjaxHelper.ashx?method=GetDeptJsonData";
    var objSelectTree = $.fn.zTree.init($("#divDeptTreeList"), setting);
    objSelectTree.setting.edit.renameTitle = "修改";
    objSelectTree.setting.edit.removeTitle = "删除";
    if ($("#hidIsMutiSelect").val() == "Y") {
        objSelectTree.setting.check.chkboxType = { "Y": "", "N": "" };
    }
    var cityObj = $("#" + id);
    var cityOffset = $("#" + id).offset();
    $("#divDeptPanel").css({ left: cityOffset.left + "px", top: cityOffset.top + cityObj.outerHeight() + "px" }).slideDown("fast");
    cityObj.attr("disabled", "true");
    _treetextboxID = id;
    $("body").bind("mousedown", onBodyDown);
}

function onBodyDown(event) {
    if (!(event.target.id == "btnSelectDept" || event.target.id == "divDeptTreeList" || event.target.id == "divDeptPanel" || $(event.target).parents("#divDeptPanel").length > 0)) {
        hideDeptMenu();
        $("#" + _treetextboxID).removeAttr("disabled");
    }
}function hideDeptMenu() {
    $("#divDeptPanel").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);
}